package com.heima.wemedia.controller.v1;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.heima.model.admin.dtos.SensitiveDto;
import com.heima.model.admin.pojos.AdSensitive;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.wemedia.pojos.WmSensitive;
import com.heima.wemedia.mapper.WmSensitiveMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;


/**
 * @author Psycho
 * @create 2022-05-02-11:46
 */
@RestController
public class WmNewsSensitiveController {

    @Autowired
    private WmSensitiveMapper wmSensitiveMapper;

    /**
     * 查询敏感词
     * @param dto
     * @return
     */
    @PostMapping("/listSensitive")
    public Page listSensitive(@RequestBody SensitiveDto dto){
        if(StringUtils.isBlank(dto.getName())){
            Page page = new Page(dto.getPage(),dto.getSize());
            return wmSensitiveMapper.selectPage(page,Wrappers.<WmSensitive>lambdaQuery().select());
        }
        Page page = new Page(dto.getPage(),dto.getSize());
        return wmSensitiveMapper.selectPage(page,Wrappers.<WmSensitive>lambdaQuery().select().like(WmSensitive::getSensitives,dto.getName()));
    }

    /**
     * 删除某个敏感词
     * @param id
     * @return
     */
    @DeleteMapping("del/{id}")
    public ResponseResult deleteOneSensitive(@PathVariable("id")Integer id){
        return ResponseResult.okResult(wmSensitiveMapper.delete(Wrappers.<WmSensitive>lambdaQuery().eq(WmSensitive::getId,id)));
    }

    /**
     * 修改某个敏感词
     * @param adSensitive
     */
    @PostMapping("/update")
    public void updateOneSensitive(@RequestBody AdSensitive adSensitive){
        if(StringUtils.isNotBlank(adSensitive.getSensitives()) && adSensitive.getId()!=null){
            WmSensitive wmSensitive = wmSensitiveMapper.selectOne(Wrappers.<WmSensitive>lambdaQuery().eq(WmSensitive::getId, adSensitive.getId()));
            wmSensitive.setSensitives(adSensitive.getSensitives());
            wmSensitiveMapper.updateById(wmSensitive);
        }
    }

    /**
     * 新增一个敏感词
     * @param adSensitive
     */
    @PostMapping("/save")
    public void saveOneSensitive(@RequestBody AdSensitive adSensitive){
        if(StringUtils.isNotBlank(adSensitive.getSensitives())){
            WmSensitive wmSensitive = new WmSensitive();
            wmSensitive.setSensitives(adSensitive.getSensitives());
            wmSensitive.setCreatedTime(new Date());
            wmSensitiveMapper.insert(wmSensitive);
        }
    }
}
